package cn.itsource.org.controller;

import cn.itsource.basic.exception.BusinessException;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.query.ShopQuery;
import cn.itsource.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop")
@Api(tags = "店铺接口",description = "店铺接口详细描述")
public class ShopController {
    @Autowired
    private IShopService shopService;

    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return AjaxResult.me();
        }catch (BusinessException e){
            //将service层的自定义异常返回给前端
            return AjaxResult.me().setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统异常，稍后重试！！");
        }
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "店铺查找",notes = "根据ID查找店铺")
    public Shop findOne(@PathVariable("id") Long id){
        Shop one = shopService.findOne(id);
        return one;
    }

    /**
     * 查询所有localhost:8080/shop
     * @return
     */
    @GetMapping
    @ApiOperation(value = "店铺查找",notes = "查找所有店铺")
    public List<Shop> findAll(){
        List<Shop> list = shopService.findAll();
        return list;
    }

    @DeleteMapping("/{id}")  /*@RequestBody  接收请求体localhost:8080/shop/4*/
    @ApiOperation(value = "删除店铺",notes = "根据ID删除店铺")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            shopService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败");
        }
    }

    @PatchMapping  /*@RequestBody  接收请求体localhost:8080/shop*/
    @ApiOperation(value = "批量删除",notes = "根据ids删除店铺")
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            shopService.patchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除失败");
        }
    }

    @PostMapping
    @ApiOperation(value = "分页查找店铺",notes = "分页查找店铺")/*localhost:8080/shop*/
    public PageList<Shop> queryForPage(@RequestBody ShopQuery shopQuery){
        PageList<Shop> pageList = shopService.queryForPage(shopQuery);
        return pageList;
    }
}
